#coding: utf-8
#1.从数据库中读取订单记录，统计出{user_id: {course_id: course_cnt/total}},即prefs[usrid][course_id] = course_cnt/total
#2.对于这种订单的情况用sim_distance是合适的
#3.使用userbased filter就可以了
#4.尝试itembased filter
#
from __future__ import division
from meal.models import Restaurant, Course, Order

def init_order_prefs():
    order_list = Order.objects.all()
    user_order_cnt = {}
    #order_list = Order.objects.exclude(user__last_name__exact='')
    prefs_cnt = {}
    user_set = set([])
    for order in order_list:
        user_set.add(order.user)
        prefs_cnt.setdefault(order.user, {})
        prefs_cnt[order.user].setdefault(order.course, 0)
        prefs_cnt[order.user][order.course] += 1
        
        user_order_cnt.setdefault(order.user, 0)
        user_order_cnt[order.user] += 1
        
    #print prefs_cnt
    #print user_set
    prefs = {}
    for user, pref_cnt in prefs_cnt.items():
        prefs.setdefault(user, {})
        for course, cnt in pref_cnt.items():
            prefs[user][course] = cnt/user_order_cnt[user]       
    
    return prefs_cnt,user_set,prefs

def init_order_group():
    prefs_cnt,user_set,prefs = init_order_prefs()
    users = [ user for user in user_set ]
    dinners_set = 